package com.v1.video.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.db.dao.VideoTB;
import com.v1.video.domain.RecommendEntity.CategoryInfo;
import com.v1.video.interfaces.OnChannelListener;
import com.v1.video.util.Helper;
import com.v1.video.util.TransferUtils;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐频道适配器
 * 
 * @author wangfang
 * 
 */
public class RecommendAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private List<CategoryInfo> mLstData;
	private int mHeight;
	private int mWidth;
	private OnChannelListener mOnChannelListener;

	public RecommendAdapter(Context context) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mLstData = new ArrayList<CategoryInfo>();
		float padding = mContext.getResources().getDimension(
				R.dimen.margin_size_13) * 2;
		float margin = mContext.getResources().getDimension(
				R.dimen.margin_size_4);
		mWidth = Utils.computeImageWidth(mContext, (int) (padding + margin), 2);
		mHeight = Utils.computeImageHeight(mContext, (int) (padding + margin),
				2);
	}

	public List<CategoryInfo> getLstData() {
		return mLstData;
	}

	public void setLstData(List<CategoryInfo> mLst) {
		if (mLst != null && mLst.size() > 0) {
			this.mLstData = mLst;
			this.notifyDataSetChanged();
		}
	}
	
	public void setOnChannelListener(OnChannelListener onChannelListener) {
		mOnChannelListener = onChannelListener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.fragment_recommend_item, null);
			holder = new Holder();
			holder.name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.more = (TextView) convertView.findViewById(R.id.tv_more);
			holder.layLine = convertView.findViewById(R.id.lay_line);
			holder.layLink = convertView.findViewById(R.id.lay_link);
			holder.mark = (TextView) convertView.findViewById(R.id.tv_mark);
			holder.linktitle = (TextView) convertView
					.findViewById(R.id.tv_linktitle);
			holder.layLink2 = convertView.findViewById(R.id.lay_link2);
			holder.mark2 = (TextView) convertView.findViewById(R.id.tv_mark2);
			holder.linktitle2 = (TextView) convertView
					.findViewById(R.id.tv_linktitle2);
			holder.layitem = convertView.findViewById(R.id.lay_item);
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.subtitle = (TextView) convertView
					.findViewById(R.id.tv_subtitle);
			holder.corner = (TextView) convertView.findViewById(R.id.tv_corner);
			holder.layitem2 = convertView.findViewById(R.id.lay_item2);
			holder.image2 = (ImageView) convertView
					.findViewById(R.id.iv_image2);
			holder.title2 = (TextView) convertView.findViewById(R.id.tv_title2);
			holder.subtitle2 = (TextView) convertView
					.findViewById(R.id.tv_subtitle2);
			holder.corner2 = (TextView) convertView.findViewById(R.id.tv_corner2);
			holder.layRow = convertView.findViewById(R.id.lay_row);
			holder.layitem3 = convertView.findViewById(R.id.lay_item3);
			holder.image3 = (ImageView) convertView
					.findViewById(R.id.iv_image3);
			holder.title3 = (TextView) convertView.findViewById(R.id.tv_title3);
			holder.subtitle3 = (TextView) convertView
					.findViewById(R.id.tv_subtitle3);
			holder.corner3 = (TextView) convertView.findViewById(R.id.tv_corner3);
			holder.layitem4 = convertView.findViewById(R.id.lay_item4);
			holder.image4 = (ImageView) convertView
					.findViewById(R.id.iv_image4);
			holder.title4 = (TextView) convertView.findViewById(R.id.tv_title4);
			holder.subtitle4 = (TextView) convertView
					.findViewById(R.id.tv_subtitle4);
			holder.corner4 = (TextView) convertView.findViewById(R.id.tv_corner4);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final CategoryInfo info = mLstData.get(position);
		if (!TextUtils.isEmpty(info.getName())) {
			holder.name.setText(info.getName());
		} else {
			holder.name.setText("");
		}
		if (info.getIsmore() == 0) {
			holder.more.setVisibility(View.GONE);
		} else {
			holder.more.setVisibility(View.VISIBLE);
		}
		if (info.getLittetop() != null && info.getLittetop().size() > 0) {
			holder.layLine.setVisibility(View.VISIBLE);
			holder.layLink.setVisibility(View.VISIBLE);
			holder.layLink2.setVisibility(View.GONE);
			for (int i = 0; i < info.getLittetop().size(); i++) {
				VideoTB littetop = info.getLittetop().get(i);
				switch (i) {
				case 0:
					if (!TextUtils.isEmpty(littetop.getMark())) {
						holder.mark.setText(littetop.getMark());
					} else {
						holder.mark.setText("");
					}
					if (!TextUtils.isEmpty(littetop.getTitle())) {
						holder.linktitle.setText(littetop.getTitle());
					} else {
						holder.linktitle.setText("");
					}
					break;
				case 1:
					holder.layLink2.setVisibility(View.VISIBLE);
					if (!TextUtils.isEmpty(littetop.getMark())) {
						holder.mark2.setText(littetop.getMark());
					} else {
						holder.mark2.setText("");
					}
					if (!TextUtils.isEmpty(littetop.getTitle())) {
						holder.linktitle2.setText(littetop.getTitle());
					} else {
						holder.linktitle2.setText("");
					}
					break;
				default:
					break;
				}
			}
		} else {
			holder.layLine.setVisibility(View.GONE);
			holder.layLink.setVisibility(View.GONE);
			holder.layLink2.setVisibility(View.GONE);
		}
		holder.layitem.setVisibility(View.VISIBLE);
		holder.layitem2.setVisibility(View.VISIBLE);
		ViewGroup.LayoutParams params = holder.image.getLayoutParams();
		params.height = mHeight;
		params.width = mWidth;
		holder.image.setLayoutParams(params);
		if (info.getVideolist() != null && info.getVideolist().size() > 0) {
			holder.image2.setVisibility(View.GONE);
			holder.title2.setVisibility(View.GONE);
			holder.subtitle2.setVisibility(View.GONE);
			holder.corner2.setVisibility(View.GONE);
			holder.layRow.setVisibility(View.GONE);
			holder.layitem3.setVisibility(View.GONE);
			holder.image3.setVisibility(View.GONE);
			holder.title3.setVisibility(View.GONE);
			holder.subtitle3.setVisibility(View.GONE);
			holder.corner3.setVisibility(View.GONE);
			holder.layitem4.setVisibility(View.GONE);
			holder.image4.setVisibility(View.GONE);
			holder.title4.setVisibility(View.GONE);
			holder.subtitle4.setVisibility(View.GONE);
			holder.corner4.setVisibility(View.GONE);
			for (int i = 0; i < info.getVideolist().size(); i++) {
				VideoTB video = info.getVideolist().get(i);
				switch (i) {
				case 0:
					holder.image.setVisibility(View.VISIBLE);
					holder.title.setVisibility(View.VISIBLE);
					if (Constant.VIDEO_TYPE_12.equals(video.getAt())
							|| Constant.VIDEO_TYPE_13.equals(video.getAt())
							|| Constant.VIDEO_TYPE_14.equals(video.getAt())
							|| Constant.VIDEO_TYPE_15.equals(video.getAt())) {
						holder.subtitle.setVisibility(View.VISIBLE);
						holder.title.setMaxLines(1);
						if (!TextUtils.isEmpty(video.getSub_title())) {
							holder.subtitle.setText(video.getSub_title());
						} else {
							holder.subtitle.setText("");
						}
					} else {
						holder.subtitle.setVisibility(View.GONE);
						holder.title.setMaxLines(2);
					}
					if (!TextUtils.isEmpty(video.getTitle())) {
						holder.title.setText(video.getTitle());
					} else {
						holder.title.setText("");
					}
					
					if (!TextUtils.isEmpty(video.getMark())) {
						holder.corner.setText(video.getMark());
						holder.corner.setVisibility(View.VISIBLE);
					} else {
						holder.corner.setVisibility(View.GONE);
					}
					if (!TextUtils.isEmpty(video.getPic())) {
						ImageLoader.getInstance().displayImage(video.getPic(),
								holder.image,
								Constant.VIDEO_10_16_OPTION_ROUNDED);
					} else {
						holder.image
								.setImageResource(R.drawable.common_place_holder_bg_16_10);
					}
					break;
				case 1:
					holder.image2.setLayoutParams(params);
					holder.image2.setVisibility(View.VISIBLE);
					holder.title2.setVisibility(View.VISIBLE);
					if (Constant.VIDEO_TYPE_12.equals(video.getAt())
							|| Constant.VIDEO_TYPE_13.equals(video.getAt())
							|| Constant.VIDEO_TYPE_14.equals(video.getAt())
							|| Constant.VIDEO_TYPE_15.equals(video.getAt())) {
						holder.subtitle2.setVisibility(View.VISIBLE);
						holder.title2.setMaxLines(1);
						if (!TextUtils.isEmpty(video.getSub_title())) {
							holder.subtitle2.setText(video.getSub_title());
						} else {
							holder.subtitle2.setText("");
						}
					} else {
						holder.subtitle2.setVisibility(View.GONE);
						holder.title2.setMaxLines(2);
					}
					if (!TextUtils.isEmpty(video.getTitle())) {
						holder.title2.setText(video.getTitle());
					} else {
						holder.title2.setText("");
					}
					if (!TextUtils.isEmpty(video.getMark())) {
						holder.corner2.setText(video.getMark());
						holder.corner2.setVisibility(View.VISIBLE);
					} else {
						holder.corner2.setVisibility(View.GONE);
					}
					if (!TextUtils.isEmpty(video.getPic())) {
						ImageLoader.getInstance().displayImage(video.getPic(),
								holder.image2,
								Constant.VIDEO_10_16_OPTION_ROUNDED);
					} else {
						holder.image2
								.setImageResource(R.drawable.common_place_holder_bg_16_10);
					}
					break;
				case 2:
					holder.layRow.setVisibility(View.VISIBLE);
					holder.layitem3.setVisibility(View.VISIBLE);
					holder.layitem4.setVisibility(View.VISIBLE);
					holder.image3.setLayoutParams(params);
					holder.image3.setVisibility(View.VISIBLE);
					holder.title3.setVisibility(View.VISIBLE);
					if (Constant.VIDEO_TYPE_12.equals(video.getAt())
							|| Constant.VIDEO_TYPE_13.equals(video.getAt())
							|| Constant.VIDEO_TYPE_14.equals(video.getAt())
							|| Constant.VIDEO_TYPE_15.equals(video.getAt())) {
						holder.subtitle3.setVisibility(View.VISIBLE);
						holder.title3.setMaxLines(1);
						if (!TextUtils.isEmpty(video.getSub_title())) {
							holder.subtitle3.setText(video.getSub_title());
						} else {
							holder.subtitle3.setText("");
						}
					} else {
						holder.subtitle3.setVisibility(View.GONE);
						holder.title3.setMaxLines(2);
					}
					if (!TextUtils.isEmpty(video.getTitle())) {
						holder.title3.setText(video.getTitle());
					} else {
						holder.title3.setText("");
					}
					if (!TextUtils.isEmpty(video.getMark())) {
						holder.corner3.setText(video.getMark());
						holder.corner3.setVisibility(View.VISIBLE);
					} else {
						holder.corner3.setVisibility(View.GONE);
					}
					if (!TextUtils.isEmpty(video.getPic())) {
						ImageLoader.getInstance().displayImage(video.getPic(),
								holder.image3,
								Constant.VIDEO_10_16_OPTION_ROUNDED);
					} else {
						holder.image3
								.setImageResource(R.drawable.common_place_holder_bg_16_10);
					}
					break;
				case 3:
					holder.image4.setLayoutParams(params);
					holder.image4.setVisibility(View.VISIBLE);
					holder.title4.setVisibility(View.VISIBLE);
					if (Constant.VIDEO_TYPE_12.equals(video.getAt())
							|| Constant.VIDEO_TYPE_13.equals(video.getAt())
							|| Constant.VIDEO_TYPE_14.equals(video.getAt())
							|| Constant.VIDEO_TYPE_15.equals(video.getAt())) {
						holder.subtitle4.setVisibility(View.VISIBLE);
						holder.title4.setMaxLines(1);
						if (!TextUtils.isEmpty(video.getSub_title())) {
							holder.subtitle4.setText(video.getSub_title());
						} else {
							holder.subtitle4.setText("");
						}
					} else {
						holder.subtitle4.setVisibility(View.GONE);
						holder.title4.setMaxLines(2);
					}
					if (!TextUtils.isEmpty(video.getTitle())) {
						holder.title4.setText(video.getTitle());
					} else {
						holder.title4.setText("");
					}
					if (!TextUtils.isEmpty(video.getMark())) {
						holder.corner4.setText(video.getMark());
						holder.corner4.setVisibility(View.VISIBLE);
					} else {
						holder.corner4.setVisibility(View.GONE);
					}
					if (!TextUtils.isEmpty(video.getPic())) {
						ImageLoader.getInstance().displayImage(video.getPic(),
								holder.image4,
								Constant.VIDEO_10_16_OPTION_ROUNDED);
					} else {
						holder.image4
								.setImageResource(R.drawable.common_place_holder_bg_16_10);
					}
					break;
				default:
					break;
				}
			}
		} else {
			holder.layitem.setVisibility(View.GONE);
			holder.layitem2.setVisibility(View.GONE);
			holder.layRow.setVisibility(View.GONE);
		}
		holder.more.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ("500".equals(info.getCid())) {
                    TransferUtils.getInstance().transferMainSecond(mContext);
				} else {
					if (mOnChannelListener != null) {
						mOnChannelListener.onChannelListener(info.getCid(),
								info.getCtype());
					}
				}
			}
		});

		holder.layLink.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getLittetop() != null
						&& info.getLittetop().size() > 0) {
					transferActivity(info.getLittetop().get(0));
				}
			}
		});

		holder.layLink2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getLittetop() != null
						&& info.getLittetop().size() > 1) {
					transferActivity(info.getLittetop().get(1));
				}
			}
		});

		holder.layitem.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideolist() != null
						&& info.getVideolist().size() > 0) {
					transferActivity(info.getVideolist().get(0));
				}
			}
		});
		holder.layitem2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideolist() != null
						&& info.getVideolist().size() > 1) {
					transferActivity(info.getVideolist().get(1));
				}
			}
		});
		holder.layitem3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideolist() != null
						&& info.getVideolist().size() > 2) {
					transferActivity(info.getVideolist().get(2));
				}
			}
		});
		holder.layitem4.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideolist() != null
						&& info.getVideolist().size() > 3) {
					transferActivity(info.getVideolist().get(3));
				}
			}
		});
		return convertView;
	}

	/**
	 * 跳转页面
	 * 
	 * @param video
	 */
	private void transferActivity(VideoTB video) {
		if (Helper.checkConnection(mContext)) {
			TransferUtils.getInstance().transferActivity(mContext,
					video.getVid(), video.getAt(), video.getTitle(),video.getVideoUrl(), video.getAdURL(), video.getPic(),
					video.getTid(), Constant.ADV_SOURCE_RECOMMEND,video.getName());
		} else {
			Toast.makeText(mContext, R.string.net_no_notclick,
					Toast.LENGTH_SHORT).show();
		}
	}

	private class Holder {
		public TextView name;
		public TextView more;
		public View layLine;
		public View layLink;
		public TextView mark;
		public TextView linktitle;
		public View layLink2;
		public TextView mark2;
		public TextView linktitle2;
		public View layitem;
		public ImageView image;
		public TextView title;
		public TextView subtitle;
		public TextView corner;
		public View layitem2;
		public ImageView image2;
		public TextView title2;
		public TextView subtitle2;
		public TextView corner2;
		public View layRow;
		public View layitem3;
		public ImageView image3;
		public TextView title3;
		public TextView subtitle3;
		public TextView corner3;
		public View layitem4;
		public ImageView image4;
		public TextView title4;
		public TextView subtitle4;
		public TextView corner4;
	}

}
